package plus;

import java.util.*;

import plus.util.Cons;

/**
 * Function Environment
 * implements scoped function lookup.
 * @author me
 *
 */
public class FEnv {
	// I want an indexed bag, but this might be the best I'll do in Java:
	Map<String, Cons<Symbol<Function>>> fnSyms = new HashMap<String, Cons<Symbol<Function>>>();
	void find_function(String name, int arity, Collection<Symbol<Function>> candidates) {
		Cons<Symbol<Function>> list = fnSyms.get(name);
		while (list != null) {
			maybe(list.head, arity, candidates);
			list = list.tail;
		}
	}
	private void maybe(Symbol<Function> sym, int arity,
			Collection<Symbol<Function>> candidates) {
		if (sym.definition.arity() == arity) candidates.add(sym);
	}
	public void enter(Symbol<Function> sym) {
		String name = sym.name;
		fnSyms.put(name, new Cons<Symbol<Function>>(sym, fnSyms.get(name)));
	}
}
